﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UMFramework.Network;

namespace Hi_Baidu.Packet
{
    class TalkMessageProtocolParser : IParser
    {
        #region IParser 成员

        public InPackage ParserInPackage(Dictionary<String,byte[]> PckContent)
        {
            TalkMessageInPackage BIP = new TalkMessageInPackage();
            //ACK Property
            if (Encoding.Default.GetString(PckContent["ACK"]) == "0")
            {
                BIP.IS_ACK = false;
            }
            else
            {
                BIP.IS_ACK = true;
            }
            //Encrypt Property
            if (Encoding.Default.GetString(PckContent["ENCRYPT"]) == "0")
            {
                BIP.IS_ENCRYPT = false;
            }
            else
            {
                BIP.IS_ENCRYPT = true;
            }
            BIP.Protocol = PackageHelper.GetProtocol(PckContent);
            BIP.SendTime = DateTime.Now;
            BIP.Header = Encoding.Default.GetString(PckContent["TYPE"]);
            BIP.Body = PckContent;
            BIP.Command = PackageHelper.GetTalkNetworkCommand(PckContent);
            if (PckContent.ContainsKey("CONTENT"))
            {
                BIP.Content = Encoding.Default.GetString(PckContent["CONTENT"]).Replace("|", " ");
            }
            return BIP;
        }

        public int GetLength(byte[] pckBody)
        {
            return pckBody.Length;
        }

        public bool NeedAck(Dictionary<String, byte[]> pck)
        {
            bool Result = Encoding.Default.GetString(pck["ACK"]) == "0" ? false : true;
             return Result;
        }

        public bool Accpet(Dictionary<String, byte[]> pck)
        {
            return PackageHelper.GetProtocol(pck) == Hi_Baidu.Packet.Protocol.NetworkProtocol.TALK_MESSAGE;
        }

        #endregion
    }
}
